﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SharePointExplorer.SharePoint;
using System.Speech;
using System.Speech.Synthesis;
using Microsoft.Speech.Synthesis;
using System.Threading;
using System.Media;

namespace SharePointExplorer.UserControls
{
    /// <summary>
    /// Interaction logic for TextReader.xaml
    /// </summary>
    public partial class TextReader : UserControl
    {
        System.Speech.Synthesis.SpeechSynthesizer synth;
        System.Speech.Synthesis.PromptBuilder builder;
        
        App app = (App)Application.Current;
        public TextReader()
        {
            InitializeComponent();
            SharePointExplorer.Classes.KinectHelper.Start();
            this.Unloaded += new RoutedEventHandler(TextReader_Unloaded);
            SharePointExplorer.Classes.KinectHelper.TogglePostureDetectedHandler(false);
            app.MainWindowInstance.DeactivateButton.Visibility = Visibility.Visible;
            app.MainWindowInstance.HomeButton.Visibility = Visibility.Visible;
            app.MainWindowInstance.backButton.Visibility = Visibility.Visible;
            synth = new System.Speech.Synthesis.SpeechSynthesizer();
            Loaded += new RoutedEventHandler(TextReader_Loaded);
        }

        void TextReader_Unloaded(object sender, RoutedEventArgs e)
        {
            SharePointExplorer.Classes.KinectHelper.Stop();
        }

        void TextReader_Loaded(object sender, RoutedEventArgs e)
        {
            string str = SharePointHelper.ReadFile(SharePointHelper.SharePointSiteUrl);
            FileText.Text = str;

            builder = new System.Speech.Synthesis.PromptBuilder(new System.Globalization.CultureInfo("en-US"));
            builder.AppendText(FileText.Text);

            app.TextToRead = FileText.Text;
        }

        public TextReader(bool ReadText) : this()
        {
           
        }

        public void Speak(object o)
        {
            
          
        }

        private void btnRead_Click(object sender, RoutedEventArgs e)
        {
            btnRead.IsEnabled = false;
            
            var voice = synth.GetInstalledVoices().FirstOrDefault();

            synth.SelectVoice(voice.VoiceInfo.Name);

            

            synth.SetOutputToDefaultAudioDevice();
            synth.Volume = 100;
            synth.SpeakAsync(builder);
            synth.SpeakCompleted += new EventHandler<System.Speech.Synthesis.SpeakCompletedEventArgs>(synth_SpeakCompleted);
        }

        void synth_SpeakCompleted(object sender, System.Speech.Synthesis.SpeakCompletedEventArgs e)
        {
            btnRead.IsEnabled = true;
        }

        private void AudioButton_Click(object sender, RoutedEventArgs e)
        {
            //AudioButton.IsEnabled = false;
            ////KinectSpeechControl speechControl = new KinectSpeechControl(FileText.Text, app.MainWindowInstance.kinectSensor);
            ////speechControl.sre.RecognizeAsyncStop();
            ////speechControl.sre.RecognizeCompleted += new EventHandler<Microsoft.Speech.Recognition.RecognizeCompletedEventArgs>(sre_RecognizeCompleted);
            //app.TextToRead = FileText.Text;
            
        }

        void sre_RecognizeCompleted(object sender, Microsoft.Speech.Recognition.RecognizeCompletedEventArgs e)
        {
            if (e.Result.Confidence >= 0.7 && e.Result.Text == "Kinect Read")
            {
                
            }
            else
            {

                // DumpRecordedAudio(e.Result.Audio);
            }
        }


    }
}
